package cn.tedu.dental.admin.service.impl;

import cn.tedu.dental.admin.mapper.AdminMapper;
import cn.tedu.dental.admin.pojo.dto.DoctorLoginParam;
import cn.tedu.dental.admin.pojo.dto.DoctorQuery;
import cn.tedu.dental.admin.pojo.dto.DoctorSaveParam;
import cn.tedu.dental.admin.pojo.entity.Doctor;
import cn.tedu.dental.admin.pojo.vo.DoctorVO;
import cn.tedu.dental.admin.service.AdminService;
import cn.tedu.dental.base.exception.ServiceException;
import cn.tedu.dental.base.response.StatusCode;
import cn.tedu.dental.user.pojo.dto.UserQuery;
import cn.tedu.dental.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class AdminServiceImpl implements AdminService {
    @Autowired
    AdminMapper adminMapper;

    @Override
    public List<DoctorVO> selectDoctor(DoctorQuery doctorQuery) {
        List<DoctorVO> list = adminMapper.selectDoctor(doctorQuery);
        return list;
    }

    @Override
    public List<UserVO> selectUser(UserQuery userQuery) {
        List<UserVO> list = adminMapper.selectUser(userQuery);
        return list;
    }

    @Override
    public DoctorVO doctorLogin(DoctorLoginParam doctorLoginParam) {
        log.debug("医生登录，参数：{}", doctorLoginParam);
        DoctorVO doctorVO = adminMapper.doctorLogin(doctorLoginParam.getUsername());
        if (doctorVO == null) {
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!doctorVO.getPassword().equals(doctorLoginParam.getPassword())) {
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return doctorVO;
    }

    @Override
    public void saveDoctor(DoctorSaveParam doctorSaveParam) {
        Doctor doctor = new Doctor();
        BeanUtils.copyProperties(doctorSaveParam, doctor);

        if (doctor.getId() == null) {
            //新增医生时要判断该用户名是否存在
            if (adminMapper.selectByUsername(doctor.getUsername()) != null) {
                throw new ServiceException(StatusCode.USERNAME_ALREADY_EXISTS);
            }

            doctor.setRole("20");
            adminMapper.insertDoctor(doctor);
        } else {

            adminMapper.updateDoctor(doctor);
        }
    }

    @Override
    public DoctorVO selectByDoctorId(DoctorQuery doctorQuery) {
        log.debug("DoctorQuery入参={}", doctorQuery);
        DoctorVO doctorVO = adminMapper.selectByDoctorId(doctorQuery);
        log.debug("查询结果={}", doctorVO);
        return doctorVO;
    }

    @Override
    public void deleteDoctor(Long doctorId) {
        adminMapper.deleteDoctor(doctorId);
    }

    @Override
    public void resetDoctorPassword(Long doctorId) {
        DoctorVO doctorVO = new DoctorVO();
        doctorVO.setId(doctorId);
        doctorVO.setPassword("123456");
        adminMapper.updateDoctorPassword(doctorVO);

    }
}
